<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <div style="text-align:right margin-bottom:20px">
          <el-button type="primary" @click="addPermissionBtn(1,'0','add')">新增权限</el-button>
          <el-button type="primary" @click="addAsyncRouter">动态添加路由</el-button>

        </div>
      </el-card>
      <el-card>
        <el-table
          :data="list"
          row-key="id"
          style="width: 100%"
        >
          <el-table-column
            fixed
            prop="name"
            label="名称"
            width="150"
          />
          <el-table-column
            prop="code"
            label="编码"
          />
          <el-table-column
            prop="description"
            label="描述"
          />
          <el-table-column
            label="操作"
            width="200"
          >
            <template slot-scope="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="addPermissionBtn(2,scope.row.id,'add')">添加</el-button>
              <el-button type="text" @click="addPermissionBtn(scope.row.type,scope.row.id,'edit')">编辑</el-button>
              <el-button type="text" @click="deletePermissionBtn(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog
        :title="isEdit?'编辑按钮':'增加按钮'"
        :visible.sync="dialogVisible"
        width="50%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :show-close="false"
      >
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="权限名称">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="form.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="form.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch v-model="form.enVisible" active-text="启用" active-value="1" inactive-text="不启用" inactive-value="2" />
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="onSubmit">{{ isEdit?'编辑按钮':'增加按钮' }}</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getAllPermissionRequest, addPermissionRequest, deletePermissionRequest, getPermissionByIdRequest, updatePermissionRequest } from '@/api/permission'
import { transformListToTree2 } from '@/utils/index'
export default {
  data() {
    return {
      list: [],
      dialogVisible: false,
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        pid: '',
        type: 0
      },
      isEdit: false
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    // 获取所有人员权限列表
    async getPermission() {
      console.log(123)
      try {
        const res = await getAllPermissionRequest()
        console.log('getAllPermissionRequest: ', res)
        if (res.success) {
          this.list = transformListToTree2(res.data, '0')
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('getAllPermissionRequest: ', err)
      }
    },
    // 增加权限按钮
    addPermissionBtn(type, pid, isEditorAdd) {
      if (isEditorAdd === 'edit') {
        this.isEdit = true
        this.getPermissionById(pid)
      } else {
        this.addPermission()
      }
      this.form.type = type
      this.form.pid = pid
      this.dialogVisible = true
    },
    // 发送人员增加请求
    async addPermission() {
      try {
        const res = await addPermissionRequest(this.form)
        console.log('addPermissionRequest: ', res)
        if (res.success) {
          this.$message.success(res.message)
          this.dialogVisible = false
          this.getPermission()
        }
      } catch (err) {
        console.log('addPermissionRequest: ', err)
      }
    },
    // 点击编辑数据回显到表单
    async getPermissionById(id) {
      try {
        const res = await getPermissionByIdRequest(id)
        console.log('getPermissionByIdRequest: ', res)
        if (res.success) {
          this.$message.success(res.message)
          this.form = res.data
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('getPermissionByIdRequest: ', err)
      }
    },
    // 更新数据请求
    async updatePermission() {
      try {
        const res = await updatePermissionRequest(this.form)
        console.log('updatePermissionRequest: ', res)
        if (res.success) {
          this.$message.success(res.message)
          this.dialogVisible = false
          this.getPermission()
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('updatePermissionRequest: ', err)
      }
    },
    // 点击删除按钮时提示用弹框警告用户是否确认删除
    deletePermissionBtn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deletePermission(id)
        this.getPermission()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 发送删除请求
    async deletePermission(id) {
      // console.log(id)
      try {
        const res = await deletePermissionRequest(id)
        console.log('deletePermissionRequest: ', res)
        if (res.success) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log('deletePermissionRequest: ', err)
      }
    },
    // 提交按钮
    onSubmit() {
      if (this.isEdit) {
        this.updatePermission()
      } else {
        this.addPermission()
      }
      this.cancel()
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false
      this.form = this.$options.data().form
    },
    addAsyncRouter() {
      console.log(123)
      this.$router.addRoutes([
        { path: '/demo', component: () => import('@/views/demo.vue') }
      ])
    }
  }
}
</script>
